package com.domain.controller;

import com.domain.common.Comments;
import com.domain.common.Result;
import com.domain.service.AliPayService;
import com.domain.utils.JwtUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;


@Controller
@RequestMapping("/alipay")
public class AliPayController {
    @Autowired
    private AliPayService aliPayService;


    /**
     * 绑定支付宝
     *
     * @param userToken
     * @param usersId
     * @param code
     * @return
     */
    @RequestMapping(value = "/openauth", method = RequestMethod.POST)
    @ResponseBody
    public Result openauth(String userToken, String usersId, String code) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return aliPayService.openauth(usersId, code);
        } catch (Exception e) {
            return Result.fail("系统错误");
        }
    }

    /**
     * 取消绑定支付宝
     *
     * @param userToken
     * @param usersId
     * @return
     */
    @RequestMapping(value = "/deleteAlipay", method = RequestMethod.POST)
    @ResponseBody
    public Result deleteAlipay(String userToken, String usersId) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return aliPayService.deleteAlipay(usersId);
        } catch (Exception e) {
            return Result.fail("系统错误");
        }
    }


    /**
     * 支付宝支付成功后.回调该接口
     *
     * @param request
     * @return
     */
    @RequestMapping(value = "/return_url", method = RequestMethod.POST)
    @ResponseBody
    public Result return_url(HttpServletRequest request) {
        try {
            return aliPayService.return_url(request);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


    /**
     * 支付宝支付成功后修改状态
     *
     * @param userToken        用户token
     * @param usersId          用户id
     * @param publishId        发布id
     * @param acknowledgeUsers 被答谢人id
     * @param type             类型（1 补加红包支付(只传 usersId)   2 帮转红包补加支付(传 usersId, publishId)   3 完成帮忙答谢支付（传usersId, acknowledgeUsers））
     * @return
     */
    @RequestMapping(value = "/paySuccrss", method = RequestMethod.POST)
    @ResponseBody
    public Result paySuccrss(String userToken, String usersId, String publishId, String acknowledgeUsers, String money, String type) {
        try {
            //判断用户是否登录
            if (!JwtUtils.veryfiy(userToken)) {
                return Result.publicResult(Comments.NEED_LOGIN, "请登录");
            }
            return aliPayService.paySuccrss(usersId, publishId, acknowledgeUsers, money, type);
        } catch (Exception e) {
            e.printStackTrace();
            return Result.fail("系统错误");
        }
    }


}
